package com.rzj.sys_lab_assistant.controller;

import com.rzj.sys_lab_assistant.entity.Lab;
import com.rzj.sys_lab_assistant.entity.Technician;
import com.rzj.sys_lab_assistant.entity.bo.LabBO;
import com.rzj.sys_lab_assistant.service.IDepartmentService;
import com.rzj.sys_lab_assistant.service.ILabService;
import com.rzj.sys_lab_assistant.service.ITechnicianService;
import com.rzj.sys_lab_assistant.utils.ResultsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @program: sys_lab_assistant
 * @description:
 * @author: 作者
 * @create: 2021-06-29 12:04
 */
@Controller
@RequestMapping("lab")
public class LabController {

    @Autowired
    ILabService labService;
    @Autowired
    ITechnicianService technicianService;
    @Autowired
    IDepartmentService departmentService;

    ResultsUtils resultsUtils;

    @RequestMapping("toAdd")
    public String toAdd(Model model) {
        // 将技术员列表、部门列表传输给页面
        model.addAttribute("technicianList", technicianService.listTechnicians());
        model.addAttribute("departmentList", departmentService.listDepartments());
        return "lab/labAdd";
    }

    @RequestMapping("toEdit/{labId}")
    public String toUpdate(Model model, @PathVariable("labId") int labId) {
        model.addAttribute("lab", labService.getLabById(labId));
        return "lab/labUpdate";
    }

    @RequestMapping("labList")
    public String labList(Model model) {
        model.addAttribute("labList", labService.listLabs());
        return "lab/labList";
    }

    @RequestMapping("labByAccountList")
    public String labByAccountList(Model model, HttpSession session) {
        Technician technician = (Technician) session.getAttribute("technician");
        model.addAttribute("labList", labService.listLabsByAccount(technician.getAccount()));
        return "lab/labByIdList";
    }

    @RequestMapping("labByIdList")
    public String labByIdList(Model model, HttpSession session) {
        Technician technician = (Technician) session.getAttribute("technician");
        model.addAttribute("labList", labService.listLabsByAccount(technician.getAccount())); // session
        return "assistantRepair/labByIdList";
    }

    @RequestMapping("labAdd")
    @ResponseBody
    public Map<String, String> labAdd(@RequestParam("labName") String labName,
                                      @RequestParam("dId") String dId,
                                      @RequestParam("account") String account) {

        Lab lab = new Lab(labName, Integer.valueOf(dId), account);
        int flag = labService.addLab(lab);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "添加成功！");
        } else {
            return resultsUtils.resultsMap("error", "添加失败，请联系管理员！");
        }

    }

    @RequestMapping("labEdit")
    @ResponseBody
    public Map<String, String> labEdit(@RequestParam("labName") String labName,
                                       @RequestParam("labId") String labId,
                                       @RequestParam("dId") String dId,
                                       @RequestParam("account") String account) {

        Lab lab = new Lab(Integer.valueOf(labId), labName, Integer.valueOf(dId), account);

        int flag = labService.editLab(lab);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "修改成功！");
        } else {
            return resultsUtils.resultsMap("error", "修改失败，请联系管理员！");
        }

    }

    @RequestMapping("labDel")
    @ResponseBody
    public Map<String, String> labDel(@RequestParam("labId") String labId) {

        int flag = labService.delLab(Integer.valueOf(labId));
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "删除成功！");
        } else {
            return resultsUtils.resultsMap("error", "删除失败，请联系管理员！");
        }

    }

}
